package config

import (
	"fmt"

	"github.com/spf13/viper"
)

type AppConfig struct {
	Server ServerConfig `mapstructure:"server"`
	Redis  RedisConfig  `mapstructure:"redis"`
	Mongo  MongoConfig  `mapstructure:"mongo"`
	Mysql  MysqlConfig  `mapstructure:"mysql"`
	Weixin WeixinConfig `mapstructure:"weixin"`
}

type ServerConfig struct {
	Port  int  `mapstructure:"port"`
	Debug bool `mapstructure:"debug"`
}

type RedisConfig struct {
	Host     string `mapstructure:"host"`
	Port     int    `mapstructure:"port"`
	Password string `mapstructure:"password"`
	DB       int    `mapstructure:"db"`
}

type MongoConfig struct {
	Host     string `mapstructure:"host"`
	Port     int    `mapstructure:"port"`
	Username string `mapstructure:"username"`
	Password string `mapstructure:"password"`
	Database string `mapstructure:"database"`
	AuthDB   string `mapstructure:"authdb"`
}

type MysqlConfig struct {
	Host     string `mapstructure:"host"`
	Port     int    `mapstructure:"port"`
	Username string `mapstructure:"username"`
	Password string `mapstructure:"password"`
	Database string `mapstructure:"database"`
}

type WeixinConfig struct {
	AppID     string `mapstructure:"appid"`
	AppSecret string `mapstructure:"secret"`
}

var (
	App AppConfig
)

func init() {
	viper.SetConfigName("app")
	viper.SetConfigType("yaml")
	viper.AddConfigPath("./config")
	viper.AutomaticEnv()
	err := viper.ReadInConfig() // Find and read the config file
	if err != nil {             // Handle errors reading the config file
		panic(fmt.Errorf("fatal error config file: %w", err))
	}
	// fmt.Println("===> ", viper.GetString("server.port"))

	if err = viper.Unmarshal(&App); err != nil {
		panic(fmt.Errorf("fatal error config file: %w", err))
	}
	// fmt.Println("===> ", App.Server)
}
